fix(events): prevent deadlock when closing or waiting for listeners from within a listener - #2088
Open
vdusek wants to merge 7 commits into
Open
fix(events): prevent deadlock when closing or waiting for listeners from within a listener#2088vdusek wants to merge 7 commits into
vdusek wants to merge 7 commits into
Conversation
…rom within a listener
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2088 +/- ##
==========================================
+ Coverage 93.57% 93.64% +0.06%
==========================================
Files 181 181
Lines 12590 12595 +5
==========================================
+ Hits 11781 11794 +13
+ Misses 809 801 -8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Pijukatel
reviewed
Jul 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Calling
EventManager.wait_for_all_listeners_to_complete()- or closing the manager via__aexit__- from within an event listener deadlocked. The listener runs in a task that is itself registered in_listener_tasks, so the wait ends up awaiting the very task that is awaiting it. Under a close timeout this cycle degrades further into aRecursionError.Changes in
EventManager:wait_for_all_listeners_to_complete()are tracked in_waiting_listener_tasksand excluded from the wait, so listener waiters never await themselves or each other. A caller that is not a listener is outside the cycle and still awaits every listener, waiting ones included.emitonly schedules the listener wrappers, and each registers its listener task once it starts running, so the wait yields before snapshotting_listener_tasks. This also drops theEvent listener raised an exception.log line, which duplicated the ERROR the listener wrapper already logs with the traceback.finallyusesset.discard()instead ofset.remove(), since__aexit__may have already cleared the task set while the listener was mid-flight (avoids a spuriousKeyError).Regression tests cover waiting from within a listener, several listeners waiting at once, closing the manager from within a listener, and waiting from outside while a listener is itself waiting.
This unblocks apify/apify-sdk-python#1061, where
Actor.exit()is called from inside an event listener (e.g. anABORTINGhandler) - with this fix the SDK can drop its_detach_current_listener_taskworkaround.✍️ Drafted by Claude Code